package org.xxpay.manage.config.ctrl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.PayInterfaceType;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: dingzhiwei
 * @date: 18/05/03
 * @description: 支付接口类型
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/pay_interface_type")
public class PayInterfaceTypeController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayInterfaceType payInterfaceType = getObject(param, PayInterfaceType.class);
        int count = rpcCommonService.rpcPayInterfaceTypeService.count(payInterfaceType);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<PayInterfaceType> payInterfaceTypeList = rpcCommonService.rpcPayInterfaceTypeService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), payInterfaceType);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(payInterfaceTypeList, count));
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String ifTypeCode = getStringRequired(param, "ifTypeCode");
        final String utf8Str = URLDecoder.decode(String.valueOf(ifTypeCode));
        PayInterfaceType payInterfaceType = rpcCommonService.rpcPayInterfaceTypeService.findByCode(utf8Str);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(payInterfaceType));
    }

    @RequestMapping("/update")
    @ResponseBody
    @MethodLog( remark = "修改支付接口类型" )
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayInterfaceType payInterfaceType = getObject(param, PayInterfaceType.class);
        int count = rpcCommonService.rpcPayInterfaceTypeService.update(payInterfaceType);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/add")
    @ResponseBody
    @MethodLog( remark = "新增支付接口类型" )
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayInterfaceType payInterfaceType = getObject(param, PayInterfaceType.class);
        int count = rpcCommonService.rpcPayInterfaceTypeService.add(payInterfaceType);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/deleteList")
    @ResponseBody
    public ResponseEntity<?> deleteList(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String payInterfaceTypeListStr = param.getString("payInterfaceTypeList");
        JSONArray payInterfaceTypeList = new JSONArray();
        if (payInterfaceTypeListStr != null && !payInterfaceTypeListStr.isEmpty()) {
            String[] ids = payInterfaceTypeListStr.split(",");
            for (String id : ids) {
                payInterfaceTypeList.add(id);
            }
        }
        AtomicInteger count = new AtomicInteger();
        payInterfaceTypeList.stream().filter(Objects::nonNull).forEach(e -> {
            PayInterfaceType payInterfaceType = new PayInterfaceType();
            payInterfaceType.setIfTypeCode((String) e);
            count.set(rpcCommonService.rpcPayInterfaceTypeService.delete(payInterfaceType));
        });
        if (count.get() >= 1) {
            return ResponseEntity.ok(BizResponse.buildSuccess());
        }
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

}
